Public Class square ''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' declare instance variables of square ''''''''''''''''''''''''''''''''''''''''''''''''''''''' Private side As Integer Private Xloc As Integer Private Yloc As Integer Private name As String ' text inside square Private bcolor As Color ' background color Private fcolor As Color ' foreground (text) color Private sqDisplay As System.Windows.Forms.Button ' button that will be the visual display of the square Const adjustAmount As Integer = 10 ' distance a move of "1" is in pixels Const titleBarSize As Integer = 34 ''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' declare interface to properties ''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' length of a side Public Property sideLen() As Integer Get Return side End Get Set(ByVal Value As Integer) If Value > 0 Then side = Value End If ' don't change if invalid End Set End Property ' horizontal location Public Property xLocation() As Integer Get Return Xloc End Get Set(ByVal Value As Integer) If Value >= 0 Then Xloc = Value End If ' if invalid don't change End Set End Property ' vertical location Public Property yLocation() As Integer Get Return Yloc End Get Set(ByVal Value As Integer) If Value >= 0 Then Yloc = Value End If ' if invalid don't change End Set End Property ' text inside the square Public Property sqName() As String Get Return name End Get Set(ByVal Value As String) ' don't need to validate name = Value End Set End Property ' background color Public Property backgroundColor() As Color Get Return bcolor End Get Set(ByVal Value As Color) If Value.IsKnownColor Then ' color known to VB bcolor = Value End If ' don't change if invalid End Set End Property ' text color Public Property foregroundColor() As Color Get Return fcolor End Get Set(ByVal Value As Color) If Value.IsKnownColor Then ' color known to VB fcolor = Value End If ' don't change if invalid End Set End Property ' button to use to display the square Public Property squareDisplay() As System.Windows.Forms.Button Get Return sqDisplay End Get Set(ByVal Value As System.Windows.Forms.Button) If Not Value Is Nothing Then ' check if the button exists sqDisplay = Value End If ' don't change if invalid End Set End Property ''''''''''''''''''''''''''''''''''''''''''''''''''''''' '' constructors ''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' no default constructor - because at minimun we need that button that we ' will use as the display of the square Public Sub New(ByRef sq As System.Windows.Forms.Button) sqDisplay = sq ' use values from the default button for all other instance variables side = sq.Height Xloc = sq.Left Yloc = sq.Top name = sq.Text bcolor = sq.BackColor fcolor = sq.ForeColor End Sub ' constructor using just a button and side length Public Sub New(ByRef sq As System.Windows.Forms.Button, ByVal len As Integer) sqDisplay = sq side = len ' use values from the default button for all other instance variables Xloc = sq.Left Yloc = sq.Top name = sq.Text bcolor = sq.BackColor fcolor = sq.ForeColor End Sub ' constructor using just button, side length and text Public Sub New(ByRef sq As System.Windows.Forms.Button, ByVal len As Integer, ByVal nam As String) sqDisplay = sq side = len name = nam ' use values from the default button for all other instance variables Xloc = sq.Left Yloc = sq.Top bcolor = sq.BackColor fcolor = sq.ForeColor End Sub ' constructor using just button, side length, location and text Public Sub New(ByRef sq As System.Windows.Forms.Button, ByVal len As Integer, ByVal x As Integer, ByVal y As Integer, ByVal nam As String) sqDisplay = sq side = len Xloc = x Yloc = y name = nam ' use values from the default button for all other instance variables bcolor = sq.BackColor fcolor = sq.ForeColor End Sub ' constructor passed values for all instance variables Public Sub New(ByRef sq As System.Windows.Forms.Button, ByVal len As Integer, ByVal x As Integer, ByVal y As Integer, ByVal nam As String, ByVal back As Color, ByVal fore As Color) sqDisplay = sq side = len Xloc = x Yloc = y name = nam bcolor = back fcolor = fore End Sub ''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' some useful functions ''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' calculate area of the square Public Function calcArea() As Integer Return side * side End Function ' calculate perimeter of the square Public Function calcPerim() As Integer Return side * 4 End Function ' move the square up by given amount of space - but make sure it doesn't go out of sight Public Sub moveUp(ByVal amt As Double) Yloc = Yloc - (amt * adjustAmount) If (Yloc < 0) Then Yloc = 0 End If ' adjust display to reflect changed position adjustSquare() End Sub ' move the square down by given amount of space Public Sub moveDown(ByVal amt As Double) Yloc = Yloc + (amt * adjustAmount) ' square doesn't know how big window can be - so will let it just go off ' adjust display to reflect changed position adjustSquare() End Sub ' move the square left by given amount of space - but make sure it doesn't go out of sight Public Sub moveLeft(ByVal amt As Double) Xloc = Xloc - (amt * adjustAmount) ' make sure don't go off left edge If Xloc < 0 Then Xloc = 0 End If ' adjust display to reflect changed position adjustSquare() End Sub ' move the square right by given amount of space Public Sub moveRight(ByVal amt As Double) Xloc = Xloc + (amt * adjustAmount) ' square doesn't know how big window can be - so will let it just go off ' adjust display to reflect changed position adjustSquare() End Sub ' adjust display so that it reflects instance variables Public Sub adjustSquare() sqDisplay.Height = side sqDisplay.Width = side sqDisplay.Top = Yloc sqDisplay.Left = Xloc sqDisplay.BackColor = bcolor sqDisplay.ForeColor = fcolor sqDisplay.Text = name End Sub End Class